package com.ruyuan.commodity.builder;

import cn.hutool.json.JSONUtil;
import com.ruyuan.commodity.common.mamager.GoodsInfoGalleryManager;
import com.ruyuan.commodity.enums.*;
import com.ruyuan.commodity.model.goods.dos.GoodsDO;
import com.ruyuan.commodity.model.goods.dos.GoodsGalleryDO;
import com.ruyuan.commodity.model.goods.request.GoodsInfoRequest;
import com.ruyuan.commodity.model.goods.vo.GoodsMobileIntroVO;
import com.ruyuan.commodity.open.api.aspect.UserInfo;
import com.ruyuan.commodity.open.api.client.shop.api.ShopOpenApi;
import com.ruyuan.commodity.open.api.model.goods.enums.GoodsType;
import com.ruyuan.commodity.open.api.model.shop.vo.ShopVO;
import com.ruyuan.commodity.open.api.model.util.DateUtil;
import com.ruyuan.commodity.open.api.util.AuthContextUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 商品信息构造器
 **/
@Component
public class GoodsInfoBuilder {

    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(GoodsInfoBuilder.class);
    /**
     * 店铺信息的open api
     */
    @Autowired
    private ShopOpenApi shopOpenApi;
    /**
     * 商品相册的业务管理器
     */
    @Autowired
    private GoodsInfoGalleryManager goodsInfoGalleryManager;

    /**
     * 商品文档
     */
    private final GoodsDO goods = new GoodsDO();

    public GoodsInfoBuilder buildGoodsBaseInfo(GoodsInfoRequest request) {
        UserInfo userInfo = AuthContextUtil.getUserInfo();

        goods.setGoodsId(request.getGoodsId());
        goods.setCategoryId(request.getCategoryId());
        goods.setBrandId(request.getBrandId());
        goods.setGoodsName(request.getGoodsName());
        goods.setSn(request.getSn());
        goods.setPrice(request.getPrice());
        goods.setCost(request.getCost());
        goods.setMktprice(request.getMktprice());
        goods.setWeight(request.getWeight());
        goods.setGoodsTransfeeCharge(request.getGoodsTransfeeCharge());
        goods.setIntro(request.getIntro());

        if (ObjectUtils.isEmpty(request.getHaveSpec())) {
            goods.setHaveSpec(SpecEnums.NO.getCode());
        } else {
            goods.setHaveSpec(request.getHaveSpec());
        }
        goods.setTemplateId(request.getTemplateId());
        if (StringUtils.isEmpty(request.getPageTitle())) {
            goods.setPageTitle(request.getGoodsName());
        } else {
            goods.setPageTitle(request.getPageTitle());
        }
        if (StringUtils.isEmpty(request.getMetaKeywords())) {
            goods.setMetaKeywords(request.getGoodsName());
        } else {
            goods.setMetaKeywords(request.getMetaKeywords());
        }

        if (StringUtils.isEmpty(request.getMetaDescription())) {
            goods.setMetaDescription(request.getGoodsName());
        } else {
            goods.setMetaDescription(request.getMetaDescription());
        }

        goods.setMarketEnable(!GoodsMarketEnableEnums.MARKET_ENABLE_UP.getCode().equals(request.getMarketEnable()) ?
                GoodsMarketEnableEnums.MARKET_ENABLE_DOWN.getCode() :
                GoodsMarketEnableEnums.MARKET_ENABLE_UP.getCode());

        List<GoodsMobileIntroVO> introList = request.getIntroList();
        if (introList != null && introList.size() != 0) {
            goods.setMobileIntro(JSONUtil.toJsonStr(introList));
        } else {
            goods.setMobileIntro("");
        }

        goods.setGoodsVideo(request.getGoodsVideo());
        // 判断商品是否需要审核
        goods.setIsAuth(GoodsAuthStatusEnums.AUTH_STATUS_VERIFY_SUCCESS.getCode());
        // 商品状态 是否可用
        goods.setDisabled(GoodsDisableStatusEnums.NO_DEL.getCode());

        // 商品浏览次数
        goods.setViewCount(0);
        // 商品购买数量
        goods.setBuyCount(0);
        // 评论次数
        goods.setCommentNum(0);
        // 商品评分
        goods.setGrade(100.0);
        // 商品最后更新时间
        goods.setLastModify(DateUtil.getDateline());
        // 商品库存
        goods.setQuantity(request.getQuantity() == null ? 0 : request.getQuantity());
        // 可用库存
        goods.setEnableQuantity(goods.getQuantity());

        ShopVO shop = shopOpenApi.getShop(userInfo.getSellerId());
        // 判断是否添加的是积分商品
        if (request.getExchange() != null &&
                GoodsMarketEnableEnums.MARKET_ENABLE_UP.getCode().equals(request.getExchange().getEnableExchange())) {
            goods.setGoodsType(GoodsType.POINT.name());
        } else {
            goods.setGoodsType(GoodsType.NORMAL.name());
        }

        goods.setShopCatId(request.getShopCatId());
        goods.setSellerId(userInfo.getSellerId());
        // 商品的店铺名称
        goods.setSellerName(shop.getShopName());
        // 商品创建时间
        goods.setCreateTime(DateUtil.getDateline());

        return this;
    }

    /**
     * 商品相册信息构造
     *
     * @param request 商品信息
     * @return 商品信息构造器
     */
    public GoodsInfoBuilder buildGoodsImgInfo(GoodsInfoRequest request) {
        // 向goods加入图片
        GoodsGalleryDO goodsGalley = goodsInfoGalleryManager
                .getGoodsGallery(request.getGoodsGalleryList().get(0).getOriginal());
        goods.setOriginal(goodsGalley.getOriginal());
        goods.setBig(goodsGalley.getBig());
        goods.setSmall(goodsGalley.getSmall());
        goods.setThumbnail(goodsGalley.getThumbnail());

        return this;
    }

    /**
     * 商品相册信息构造
     *
     * @param request 商品信息入参
     * @return 商品信息构造器
     */
    public GoodsInfoBuilder buildEditGoodsImgInfo(GoodsInfoRequest request) {
        // 修改相册信息
        List<GoodsGalleryDO> goodsGalleys = request.getGoodsGalleryList();
        this.goodsInfoGalleryManager.edit(goodsGalleys, request.getGoodsId());
        // 向goods加入图片
        goods.setOriginal(goodsGalleys.get(0).getOriginal());
        goods.setBig(goodsGalleys.get(0).getBig());
        goods.setSmall(goodsGalleys.get(0).getSmall());
        goods.setThumbnail(goodsGalleys.get(0).getThumbnail());

        return this;
    }

    /**
     * 商品规格相关信息信息构造 编辑时使用
     * @param goodsDO db查出来的商品信息
     * @return 商品信息构造器
     */
    public GoodsInfoBuilder buildGoodsSpecs(GoodsDO goodsDO) {
        // 查询商品是否存在的规格信息，存在则将规格中最低的价格赋值到商品价格中
        if (goodsDO.getHaveSpec() != null && GoodsHaveSpecEnums.HAVE_SPEC_YES.getCode().equals(goodsDO.getHaveSpec())) {
            // 有规格则商品价格等不变，以规格的为主
            goods.setPrice(goodsDO.getPrice());
            goods.setCost(goodsDO.getCost());
            goods.setWeight(goodsDO.getWeight());
        }
        return this;
    }

    public GoodsDO create() {
        return goods;
    }

}
